import Vue from 'vue'
import VueRouter from 'vue-router'

import cookies from 'vue-cookies'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/login',
    redirect: '/',
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/register',
    component: () => import('../views/Register.vue'),
  },
  {
    path: '/home',
    component: () => import('../views/Home.vue'),
  },
  {
    path: '*',
    component: () => import('../views/404.vue'),
  },
]

const router = new VueRouter({
  mode: 'history',
  routes,
})
// 导航守卫
// router.beforeEach((to, from, next) => {
//   // 1.如果去往登录页则放行
//   if (to.path == '/login') {
//     if (cookies.get('status')) {
//       next('/home')
//     } else {
//       next()
//     }
//   } else {
//     // 2.如果不去登录页,则判断是否哦有cookie
//     // 2.1如果有cookie, 则继续
//     if (cookies.get('status')) {
//       next()
//     } else {
//       // 2.2如果没有cookie, 如果去注册页则放行, 如果不去注册页则返回至login页
//       if (to.path == '/register') {
//         next()
//       } else {
//         next('/login')
//       }
//     }
//   }
// })

export default router
